package com.lpf.business.controller;

import com.lpf.business.service.FeignClientService;
import com.lpf.business.service.XxxService;
import com.lpf.common.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping(value = "/xxx")
public class XxxController {

    @Autowired
    private XxxService xxxService;
    @Autowired
    private FeignClientService feignClientService;

    @ApiOperation(value = "发起流程", notes = "发起流程")
    @GetMapping(value = "/start/{businessKey}")
    public Object start(@PathVariable(value = "businessKey") String businessKey) {

        // 获取userID和deplyId
        String userId = "admin001";
        String deplyId = "test-group:1:4";

        // 调用流程引擎：让数据与流程进行绑定，并启动流程
        Result<Map<String, String>> result = feignClientService.start(deplyId, userId, businessKey);

        // 更新审核状态
        xxxService.updateAuditStatus("start_audit_status");

        return result;
    }

    @ApiOperation(value = "获取审核列表", notes = "获取审核列表")
    @GetMapping(value = "/getApproveList")
    public Object getApproveList() {

        // 获取用户所属的组（或角色、或部门）
        List<String> groupIds = Arrays.asList("group01", "group01");
        // 获取用户账号
        String userId = "lpf";

        // 调用流程引擎：根据用户组获取需要审核的数据对应的流程信息
        List<Map<String, Object>> idListByGroupMapList = feignClientService.getRuntimeBusinessKeyByGroup(groupIds).getData();
        // 调用流程引擎：获取自己发起的正在进行的审核数据对应的流程信息
        List<Map<String, Object>> idListByUserMapList = feignClientService.getRuntimeBusinessKeyByUserId(userId).getData();
        // 合并两个List
        idListByUserMapList.addAll(idListByGroupMapList);

        // 收集业务键
        List<String> idList = new ArrayList<>();
        idListByUserMapList.forEach(idListByUserMap -> {
            // businessKey 为业务键
            idList.add((String) idListByUserMap.get("businessKey"));
        });

        // 根据业务键集合（idList）获取正在审核的数据
        List<Object> xxxList = xxxService.getXxxList(idList);

        // 数据整合，将需要在前端展示的流程信息与业务数据信息组合到一起
        xxxList.forEach(xxx -> {
            idListByUserMapList.forEach(idListByUserMap -> {

            });
        });

        return Result.data(xxxList);
    }

    @ApiOperation(value = "进行审核", notes = "进行审核")
    @GetMapping(value = "/task/{taskId}/{businessKey}")
    public Object taskByAssignee(@PathVariable(value = "taskId") String taskId, @PathVariable(value = "businessKey") String dataId) {

        String userId = "wt";

        // 获取组（或角色、或部门）中的用户
        List<String> userIds = Collections.emptyList();

        // 调用流程引擎：设置会签所需的用户列表数据
        feignClientService.setListVariable(taskId, "assigneeList", userIds);
        // 调用流程引擎：多实例基数，没用到
        feignClientService.setVariable(taskId, "cycleNum", userIds.size());
        // 调用流程引擎：第二审核人节点，设置审核人参数
        feignClientService.setVariable(taskId, "reviewer", "bbb");

        // 流程完成所需的条件参数
        // 主要用于第三审核人节点，根据审核人进行分支判断依据
        Map<String, Object> map = new HashMap<>();
        map.put("assignee", userId);

        // 调用流程引擎：完成任务，taskId 任务节点 ID
        feignClientService.taskByAssignee(taskId, userId, map);

        // 调用流程引擎：根据任务节点Id，获取流程实例 Id
        String processInstanceId = feignClientService.getProcessInstanceIdByTaskId(taskId).getData();
        // 调用流程引擎：通过流程实例 Id，判断流程是否结束
        boolean isFinish = feignClientService.checkProcessInstanceFinish(processInstanceId).getData();
        if (isFinish) {
            // 更新审核状态
            xxxService.updateAuditStatus("complete_status");
        }

        return Result.success("操作成功");
    }

    @ApiOperation(value = "获取审核历史记录", notes = "获取审核历史记录")
    @GetMapping(value = "/getApproveHistory")
    public Object getApproveHistory() {

        String userId = "wt";

        // 调用流程引擎：获取审核人为当前用户的已完成的任务
        List<Map<String, Object>> historys = feignClientService.getHistoryByUser(userId).getData();

        return Result.data(historys);
    }
}
